package cn.xiangxiang.service.impl;

import cn.xiangxiang.mapper.order.OrderMapper;
import cn.xiangxiang.pojo.order.Order;
import cn.xiangxiang.pojo.order.OrderItem;
import cn.xiangxiang.pojo.order.OrderLog;
import cn.xiangxiang.pojo.order.OrderVoItem;
import cn.xiangxiang.service.order.OrderItemService;
import cn.xiangxiang.service.order.OrderLogService;
import cn.xiangxiang.service.order.OrderService;
import cn.xiangxiang.service.utils.IdWorker;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private OrderLogService orderLogService;

    @Override
    public Page<Order> findConditionPage(Integer startpage, Integer pagesize, Order order) {
        QueryWrapper<Order> wrapper = getCondition(order);
        Page<Order> page = new Page<>(startpage, pagesize);
        Page<Order> orderPage = page(page, wrapper);
        return orderPage;
    }

    @Override
    public List<Order> findAll() {
        return list();
    }


    public Order findById(String id) {
        return getById(id);
    }

    @Transactional
    public void genOrder(OrderVoItem ordervoitem) {
        Order order = ordervoitem.getOrder();
        order.setId(idWorker.nextId()+"");
        order.setOrderStatus("0");
        order.setPayStatus("0");
        order.setCreateTime(new Date());
        order.setConsignStatus("0");
        order.setIsDelete("0");

        save(order);

        List<OrderItem> orderItems = ordervoitem.getOrderItems();

        orderItems.forEach((orderItem)->{
            orderItem.setId(idWorker.nextId()+"");
            orderItem.setOrderId(order.getId());
            orderItem.setIsReturn("0");
        });

        orderItemService.saveBatch(orderItems);

        // 生成日志
        genOrderLog(order);

    }

    @Override
    public void sendOutGoods(Order order) {
        String payStatus = order.getPayStatus();
        String consignStatus = order.getConsignStatus();
        String orderStatus = order.getOrderStatus();
        if("1".equals(payStatus) && "1".equals(orderStatus) && "0".equals(consignStatus)){
            order.setUpdateTime(new Date());
            order.setConsignTime(new Date());
            order.setOrderStatus("2");
            order.setConsignStatus("1");
            updateById(order);

            // 生成日志
            genOrderLog(order);
        }else{
            throw  new RuntimeException("系统状态不对，请重新发货");
        }
    }

    private void genOrderLog(Order order) {
        OrderLog orderLog = new OrderLog();
        orderLog.setId(idWorker.nextId()+"");
        orderLog.setOrderId(order.getId());
        orderLog.setOperater("system");
        orderLog.setOperateTime(order.getUpdateTime());
        orderLog.setOrderStatus(order.getOrderStatus());
        orderLog.setPayStatus(order.getPayStatus());
        orderLog.setConsignStatus(order.getConsignStatus());
        orderLog.setRemarks("无");
        orderLogService.save(orderLog);
    }

    // 定完完成
    public void finish(String id) {
        Order order = getById(id);
        // 判断-》 略
        order.setEndTime(new Date());
        order.setUpdateTime(new Date());
        order.setOrderStatus("3");

        genOrderLog(order);
    }

    // 退款
    public void returnMoney(String id) {

    }
    // 退货


    // 查询所有订单及其对应的所有订单详情列表
    public List<OrderVoItem> findOrderVoItemList() {

        List<OrderVoItem> orderVoItemList = new ArrayList<>();

        List<Order> orderList = list();

        List<OrderItem> orderItemList = orderItemService.list();

        orderList.forEach((order)->{
            OrderVoItem voItem = new OrderVoItem();
            voItem.setOrder(order);
            voItem.setOrderItems(findByOrderItem(orderItemList,order.getId()));
            orderVoItemList.add(voItem);
        });
        return orderVoItemList;
    }

    @Override
    public void closeOrderOutTime() {

    }

    /**
     * 根据订单id，查询对应的订单详情
     * */
    public OrderVoItem findOrderVoItemByOrderId(String id) {
        OrderVoItem voItem = new OrderVoItem();

        Order order = getById(id);
        voItem.setOrder(order);

        HashMap<String, Object> map = new HashMap<>();
        map.put("order_id",id);
        List<OrderItem> orderItems = orderItemService.listByMap(map);
        voItem.setOrderItems(orderItems);

        return voItem;
    }

    private List<OrderItem> findByOrderItem(List<OrderItem> orderItemList,String orderId){
        ArrayList<OrderItem> orderItems = new ArrayList<>();
        orderItemList.forEach((item)->{
            if(orderId.equals(item.getOrderId())){
                orderItems.add(item);
            }
        });
        return orderItems;

    }


    private QueryWrapper<Order> getCondition(Order order){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        if(order!=null){
            if(order.getPayType()!=null && !"".equals(order.getPayType().trim())){
                wrapper.eq("pay_type",order.getPayType());
            }
            if(order.getShippingCode()!=null && !"".equals(order.getShippingCode().trim())){
                wrapper.eq("shipping_code",order.getShippingCode());
            }
            if(order.getUsername()!=null && !"".equals(order.getUsername().trim())){
                wrapper.like("username",order.getUsername());
            }
            if(order.getOrderStatus()!=null && !"".equals(order.getOrderStatus().trim())){
                wrapper.eq("order_status",order.getOrderStatus());
            }
            if(order.getPayStatus()!=null && !"".equals(order.getPayStatus().trim())){
                wrapper.eq("pay_status",order.getPayStatus());
            }
            if(order.getConsignStatus()!=null && !"".equals(order.getConsignStatus().trim())){
                wrapper.eq("consign_status",order.getConsignStatus());
            }
        }
        return wrapper;
    }
}
